<template>
	<view class="product">
		<view class="center">
			<view class="pro_switch">
				<u-subsection :list="list" mode="subsection" :current="current" @change="sectionChange" fontSize="25rpx"
					activeColor="#e14254"></u-subsection>
			</view>

			<!-- 搜索 -->
			<view class="search">
				<u-search v-model='search' searchIconSize="35" inputAlign="center" placeholder='请输入渠道进行查询'
					:showAction="false" height="50" bgColor="white" :clearabled="true"
					@search="handleSearch"></u-search>
			</view>
			<!-- 列表1 -->
			<view class="u-page">
				<u-list @scrolltolower="scrolltolower">
					<u-list-item v-for="(item, index) in indexList" :key="index">
						<view class="order_list">
							<view class="order_list_title">
								<view class="order_num">
									订单编号：{{item.importCode}}
								</view>

							</view>
							<view class="order_list_center">
								<view class="orderSn_num">
									产品名称：{{item.productName}}
								</view>
								<view class="order_list_center_one">
									<span class="left">第三方订单号：{{item.orderId}}</span>
									<span class="right">{{getStatusText(item.orderStatus)}}</span>
								</view>
								<view class="order_list_receiver">
									<span class="receiver">收件人：{{item.receiverName}}</span><br />
									<span class="receiver">联系电话：{{maskPhoneNumber(item.receiverMobile)}}</span><br />
									<span v-if="current=='0'" class="receiver">订单来源：{{getplatFormText(item.orderPlatform)}}</span>
									<span v-if="current=='1'" class="receiver">订单来源：{{item.orderPlatform}}</span>
								</view>
							</view>
							<view class="order_list_bottom">
								<view class="btn_group">
									<button class="btn1">修改订单</button>
									<button class="btn2">提交审核</button>
									<button class="btn3">订单取消</button>
									<span class="btntext">查看详情</span>
								</view>
							</view>
						</view>
					</u-list-item>
				</u-list>
			</view>
		</view>
		<!-- 版心结束 -->
		<!-- 加载动画 -->
		<view>
			<u-toast ref="uToast"></u-toast>
		</view>
		<!-- 加在动画结束 -->
		<!-- 详细规则的弹出框 -->
		<view class="modal">
			<u-modal :show="show" @confirm="confirm" :title="title" :content='content'></u-modal>
		</view>
		<!-- 详细规则结束 -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: ['我的订单', '代理商订单'],
				current: 0,
				newState: 1, //顶部tab切换
				title: '标题',
				content: '佣金规则是：10元',
				search: '', //搜索v-model
				// 列表
				indexList: [],
				pageNum: 1,
				pageSize: 10,
				total: 100, // 替换为实际的总数据量
				loading: false, // 控制加载动画的变量

				// 加在动画
				show: false,
				loadlist: [{
					type: 'loading',
					title: '正在加载',
					message: "正在加载",
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/loading.png'
				}, ]
			}
		},
		onLoad() {
			this.loadmore()
			this.sectionChange(0);
		},
		computed: {
			getIcon() {
				return path => {
					return 'https://cdn.uviewui.com/uview/example/' + path + '.png';
				}
			},
		},
		methods: {
			// 顶部商品产品切换
			sectionChange(index) {
				this.current = index;
				this.pageNum = 1;
				// console.log(this.current,'index产品情况')
				this.showToast(this.loadlist[0])
				this.indexList = []
				// 这里current是0是上架tab 1是下架tab
				const currentState = this.current == '0' ? '我的订单' : '代理商订单';
				if (currentState == '我的订单') {
					this.myorder()
				} else if (currentState == '代理商订单') {
					this.agentOrder()
				}
			},
			// 我的订单数据接口
			myorder() {
				const token = uni.getStorageSync('token');
				uni.request({
					url: 'http://101.126.42.213:9090/order/page',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						orderPlatform: 'notAgent'
					},
					method: "GET",
					header: {
						'Token': `${token}`
					},
					success: (res) => {
						console.log(res, '我的订单数据接口');
						this.loading = false; // 隐藏加载动画
						this.pageNum = res.data.current;
						this.pageSize = res.data.size;
						this.total = res.data.total;
						const newData = res.data.records;
						this.indexList = this.indexList.concat(newData);
						console.log(this.indexList, 'indexList');
						// 更新pageNum
						this.pageNum++;
						// 如果还有更多数据，继续加载

					},
					fail: (err) => {
						this.loading = false; // 隐藏加载动画
						console.log("res faile", err);
					}
				});
			},
			// 代理商订单接口
			agentOrder() {
				const token = uni.getStorageSync('token')
				uni.request({
					url: 'http://101.126.42.213:9090/agent/page',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						orderPlatform: 'agent'
					},
					method: "GET",
					header: {
						'Token': `${token}`
					},
					success: (res) => {
						console.log(res, '代理商订单数据接口');
						this.loading = false; // 隐藏加载动画
						this.pageNum = res.data.current;
						this.pageSize = res.data.size;
						this.total = res.data.total;
						const newData = res.data.records;
						this.indexList = this.indexList.concat(newData);
						console.log(this.indexList, 'indexList');
						// 更新pageNum
						this.pageNum++;
						// 如果还有更多数据，继续加载
					},
					fail: (err) => {
						this.loading = false; // 隐藏加载动画
						console.log("res faile", err);
					}
				});
			},

			// 列表加载事件
			scrolltolower() {
				if (this.hasMoreData()) {
					this.showToast(this.loadlist[0]);
					// 模拟异步请求，实际中可以替换为你的异步请求逻辑
					setTimeout(() => {
						const currentState = this.current == '0' ? '我的订单' : '代理商订单';
						if (currentState == '我的订单') {
							this.myorder()
						} else if (currentState == '代理商订单') {
							this.agentOrder()
						}
					}, 500);
				} else {
					// 没有更多数据时弹出提示
					this.showToast({
						type: 'text',
						message: '没有更多数据了',
						duration: 2000
					});
					console.log("No more data");
				}

			},
			hasMoreData() {
				// 判断是否还有更多数据需要加载
				return this.pageNum * this.pageSize < this.total; // 替换为实际的总数据量
			},
			loadmore(state, channel) {
				// console.log('state',state)
				this.loading = true; // 显示加载动画
				const currentState = this.current == '0' ? '我的订单' : '代理商订单';
				if (currentState == '我的订单') {
					this.myorder()
				} else if (currentState == '代理商订单') {
					this.agentOrder()
				}
			},

			// 加载动画
			showToast(params) {
				this.$refs.uToast.show({
					...params,
				})
			},
			// 详细规则弹出框
			open(item) {
				this.show = true
				if (item.commission == null) {
					this.$set(item, 'commission', 0);
				}
				this.content = this.content.replace('10', item.commission)
			},
			confirm() {
				this.show = false
			},
			handleSearch() {
				console.log(this.search, '触发搜索内容');
				const channel = this.search;
				this.pageNum = 1;
				this.loading = true; // 显示加载动画
				const currentState = this.current == '0' ? '我的订单' : '代理商订单';
				if (currentState == '我的订单') {
					this.myorder()
				} else if (currentState == '代理商订单') {
					this.agentOrder()
				}
			},

			// 表格中订单状态 转换
			getStatusText(status) {
				if (
					status < 0 ||
					(typeof status === "string" && parseFloat(status) < 0)
				) {
					return "订单异常";
				}
				const statusMap = {
					0: "订单入库",
					1: "已发上游",
					2: "身份审核",
					3: "开卡中",
					4: "已发货",
					5: "已完成",
					6: "已支付",
					7: "身份审核",
					8: "敏感地区待审核",
					9: "等待验证",
					10: "等待开卡",
					11: "待支付",
					12: "身份审核",
					13: "已获得卡片",
				};

				return statusMap[status] || "订单异常";
			},
			// 表格中订单来源数据转换
			getplatFormText(status) {
				const statusMap = {
					tianmao: "天猫",
					douyin: "抖音",
					kuaishou: "快手",
					pdd: "拼多多",
					wechat: "微信",
					// 其他状态映射
				};
				return statusMap[status];
			},
			// 给手机号加密
			maskPhoneNumber(phoneNumber) {
				// 检查输入是否为11位数字
				if (/^\d{11}$/.test(phoneNumber)) {
					// 提取开头三位和结尾三位
					var start = phoneNumber.slice(0, 3);
					var end = phoneNumber.slice(-3);

					// 中间五位用星号替换
					var maskedMiddle = "*****";

					// 返回格式化后的电话号码
					return start + maskedMiddle + end;
				} else {
					// 如果不是11位数字，返回原始输入
					return phoneNumber;
				}
			},
		}
	}
</script>

<style src='./order.css' scoped>
</style>